//
//  ALLViewController.m
//  CarHome
//
//  Created by lanou3g on 15/4/29.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "ALLViewController.h"
#import "ALLNavigationBar.h"
#import "InformationTableViewController.h"

#import "Province.h"
#import "City.h"
#import "AFNetworkReachabilityManager.h"


@interface ALLViewController ()<UIScrollViewDelegate,UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate>
{
    BOOL  isConnect;
}

@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) ALLNavigationBar *allNB;
@property (nonatomic, retain) NSMutableArray *urlArray;
@property (nonatomic, retain) NSString *provinceStr;

@property (nonatomic, retain) UIWindow *window;
@property (nonatomic, retain) UIView *drawerView;
@property (nonatomic, retain) UISearchBar *seachBar;
@property (nonatomic, retain) UIView *drawerView1;
@property (nonatomic, retain) UIView *drawerCityView;
@property (nonatomic, retain) UITableView *drawerProvinceTableView;
@property (nonatomic, retain) UITableView *drawerCityTableView;


@property (nonatomic, retain) NSMutableArray *allProvinceArray;
@property (nonatomic, retain) NSMutableArray *sortFirstletterArray;
@property (nonatomic, retain) NSMutableDictionary *allProvinceDict;
@property (nonatomic, retain) NSMutableArray *allCitysArray;


- (void)addAllViewsForWindow;// 把抽屉加载到window上
- (void)loadData;// 抽屉加载数据
- (void)addAllViews;// 添加7个tableViewController
- (void)initForTableViews;//初始化量抽屉上的tableViews
- (void)panGRWhenStateNotEnded:(UIPanGestureRecognizer *)sender;// 正确滑动时执行的内容
- (void)panGRWhenStateIsEnded:(UIPanGestureRecognizer *)sender;// 结束滑动的那一瞬间执行的方法
- (void)setAttributeForScrollView:(UIScrollView *)sender;//为scrollView设置属性
- (void)addTableViewForScrollView:(UIScrollView *)sender;//为scrollView添加tableView
- (void)addNavigationForSelfViewAddScrollView:(UIScrollView *)sender;//为self的view添加导航栏
- (void)swichDrawer;// 关闭抽屉
- (void)refreshPageWithCity:(City *)city;// 更新行情页面
- (void)resignFirstResponderForSearchBar;//收回搜索栏的第一响应


- (void)reach;//用AFNetWorking第三方判断是否有网络,第三方会自动上设备上查看有没有网络
- (void)haveNetWorkState;// 在有网络的条件下的操作
- (void)notNetWorkState;// 在没网络的条件下的操作


@end

@implementation ALLViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"文章";
//    [self addAllViews];
    
    [self reach];
    
//    [self initForTableViews];
//    [self addAllViewsForWindow];
//    [self loadData];
    
}

-(void)reach
{
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {

        switch (status) {
            case -1:
                isConnect = NO;
                [self notNetWorkState];
                break;
            case 0:
                isConnect = NO;
                [self notNetWorkState];
                break;
            case 1:
                isConnect = YES;
                [self haveNetWorkState];
                break;
            case 2:
                isConnect = YES;
                [self haveNetWorkState];
                break;
            default:
                break;
        }
        
    }];
}


- (void)haveNetWorkState
{
    [self addAllViews];
    [self initForTableViews];
    [self addAllViewsForWindow];
    [self loadData];
}

- (void)notNetWorkState
{
    self.view.backgroundColor = [UIColor blackColor];
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"没有网络，请联网使用" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
    
    [alertView show];
    self.navigationController.parentViewController.view.userInteractionEnabled = NO;
}

#pragma mark - 初始化量抽屉上的tableViews
- (void)initForTableViews
{
    // 获取window
    self.window = self.navigationController.parentViewController.view.window;
    // 抽屉上的ProvincetableView
    self.drawerProvinceTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 108, UISCREENWIDTH - 70, UISCREENHEIGHT - 108)];
    _drawerProvinceTableView.delegate = self;
    _drawerProvinceTableView.dataSource = self;
    // 抽屉上的drawerCityTableView
    self.drawerCityTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, UISCREENWIDTH - 70, UISCREENHEIGHT - 64)];
    _drawerCityTableView.dataSource = self;
    _drawerCityTableView.delegate = self;

}
#pragma mark - 把抽屉加载到window上
-(void)addAllViewsForWindow
{
    
    //给window添加drawerView
    self.drawerView = [[UIView alloc] initWithFrame:CGRectMake(self.window.frame.size.width,0,self.window.frame.size.width, self.window.frame.size.height)];
    [self.window addSubview:_drawerView];
    // 大透明的view
    UIView *alphaView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.window.frame.size.width, self.window.frame.size.height)];
    alphaView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
    [_drawerView addSubview:alphaView];
    // 小透明的view（因为在大透明上加手势，点击scrollView也会触发手势）
    UIView *smallAlphaView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 70, self.window.frame.size.height)];
    smallAlphaView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0];
    [alphaView addSubview:smallAlphaView];
    // 给小透明添加手势
    UITapGestureRecognizer *tapGR = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGRAction:)]autorelease];
    [smallAlphaView addGestureRecognizer:tapGR];
    // 把tablView和导航栏和搜索button放到一个view，以方便移动
    self.drawerView1 = [[UIView alloc] initWithFrame:CGRectMake(70, 0, UISCREENWIDTH - 70, UISCREENHEIGHT)];
    UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGR:)];
    [_drawerView1 addGestureRecognizer:panGR];
    [alphaView addSubview:_drawerView1];
    // 把drawerProvinceTableView添加到drawerView1
    [_drawerView1 addSubview:_drawerProvinceTableView];
    // 给ProvincetableView添加导航栏
    UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, _drawerProvinceTableView.frame.size.width, 64)];
    navigationBar.backgroundColor = [UIColor whiteColor];
    [_drawerView1 addSubview:navigationBar];
    // 给导航栏添加label
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake((_drawerProvinceTableView.frame.size.width - 40 )/ 2 - 5, 15, 80, 40)];
    titleLabel.text = @"选择省份";
    titleLabel.font = [UIFont boldSystemFontOfSize:17.0f];
    [navigationBar addSubview:titleLabel];
    // 给导航栏添加button
    UIButton *swichButton = [[UIButton alloc] initWithFrame:CGRectMake(_drawerProvinceTableView.frame.size.width - 50, 15, 50, 44)];
    [swichButton setTitle:@"关闭" forState:UIControlStateNormal];
    [swichButton setTitleColor:[UIColor colorWithRed:0.165 green:0.241 blue:1.000 alpha:1.000] forState:UIControlStateNormal];
    swichButton.font = [UIFont systemFontOfSize:15.0f];
    [swichButton addTarget:self action:@selector(ButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [navigationBar addSubview:swichButton];
    //给导航栏添加searchbar搜索栏
    self.seachBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 64, _drawerProvinceTableView.frame.size.width - 50, 44)];
    [_drawerView1 addSubview:_seachBar];
    _seachBar.placeholder = @"请输入城市名称";
    _seachBar.delegate = self;
    _seachBar.autocorrectionType = UITextAutocorrectionTypeNo;
    _seachBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
    //给导航栏添加搜索button
    UIButton *bottomButton = [[UIButton alloc] initWithFrame:CGRectMake(_drawerView1.frame.size.width - 50, 64, 50, 44)];
    bottomButton.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.660];
    [_drawerView1 addSubview:bottomButton];
    UIButton *seachButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 44)];
    seachButton.backgroundColor = [UIColor colorWithWhite:0.680 alpha:0.550];
    [seachButton setTitle:@"搜索" forState:UIControlStateNormal];
    seachButton.font = [UIFont systemFontOfSize:15.0f];
    [seachButton setTitleColor:[UIColor colorWithRed:0.165 green:0.241 blue:1.000 alpha:1.000] forState:UIControlStateNormal];
    [seachButton addTarget:self action:@selector(seachButton:) forControlEvents:UIControlEventTouchUpInside];
    [bottomButton addSubview:seachButton];
    //抽屉上的CitytableView
    self.drawerCityView = [[UIView alloc] initWithFrame:CGRectMake(UISCREENWIDTH, 0, UISCREENWIDTH - 70, UISCREENHEIGHT)];
    [_drawerView1 addSubview:_drawerCityView];
    // 把drawerCityTableView添加到drawerCityView
    [_drawerCityView addSubview:_drawerCityTableView];
    //给drawerCityTableView添加cityNavigationBar
    UIView *cityNavigationBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UISCREENWIDTH - 70, 64)];
    cityNavigationBar.backgroundColor = [UIColor colorWithWhite:1.000 alpha:0.580];
    [_drawerCityView addSubview:cityNavigationBar];
    //给drawerCityTableView添加返回backButton
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 15, 50, 44)];
    [backButton setTitle:@"返回" forState:UIControlStateNormal];
    [backButton setTitleColor:[UIColor colorWithRed:0.165 green:0.241 blue:1.000 alpha:1.000] forState:UIControlStateNormal];
    backButton.font = [UIFont systemFontOfSize:15.0f];
    [backButton addTarget:self action:@selector(backButton:) forControlEvents:UIControlEventTouchUpInside];
    [cityNavigationBar addSubview:backButton];
    // 给cityNavigationBar添加cityLabel
    UILabel *cityLabel = [[UILabel alloc] initWithFrame:CGRectMake((_drawerProvinceTableView.frame.size.width - 40 )/ 2 - 5, 15, 80, 40)];
    cityLabel.text = @"选择城市";
    cityLabel.backgroundColor = [UIColor whiteColor];
    cityLabel.font = [UIFont boldSystemFontOfSize:17.0f];
    [cityNavigationBar addSubview:cityLabel];
    //给cityNavigationBar添加关闭citySwichButton
    UIButton *citySwichButton = [[UIButton alloc] initWithFrame:CGRectMake(_drawerCityView.frame.size.width - 50, 15, 50, 44)];
    [citySwichButton setTitle:@"关闭" forState:UIControlStateNormal];
    [citySwichButton setTitleColor:[UIColor colorWithRed:0.165 green:0.241 blue:1.000 alpha:1.000]forState:UIControlStateNormal];
    citySwichButton.font = [UIFont systemFontOfSize:15.0f];
    [citySwichButton addTarget:self action:@selector(ButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [cityNavigationBar addSubview:citySwichButton];
    
}

#pragma mark - 小透明view的轻拍手势
- (void)tapGRAction:(UITapGestureRecognizer *)sender
{
    [self resignFirstResponderForSearchBar];
    [self swichDrawer];
}


#pragma mark - 返回按钮的绑定事件
- (void)backButton:(UIButton *)sender
{
    [self resignFirstResponderForSearchBar];
    _drawerCityView.frame = CGRectMake(UISCREENWIDTH, 0, UISCREENWIDTH - 70, UISCREENHEIGHT);
}


#pragma mark - 关闭按钮的绑定事件 city关闭和province关闭
- (void)ButtonAction:(UIButton *)sender
{
    [self resignFirstResponderForSearchBar];
    [self swichDrawer];
}

#pragma mark - 关闭抽屉
- (void)swichDrawer
{
    [UIView animateWithDuration:0.3 animations:^{
        _drawerView.frame = CGRectMake(self.window.frame.size.width,0,self.window.frame.size.width, self.window.frame.size.height);
    }];
    _drawerCityView.frame = CGRectMake(UISCREENWIDTH, 0, UISCREENWIDTH - 70, UISCREENHEIGHT);
}

#pragma mark - ProvincetableView搜索按钮
- (void)seachButton:(UIButton *)sender
{
       for (Province *p in _allProvinceArray) {
        
        for (City *c in p.citys) {
            if ([c.name isEqualToString:_seachBar.text]) {
                
                [self swichDrawer];
                [self refreshPageWithCity:c];
            }
        }
    }
    
    [self resignFirstResponderForSearchBar];

}

#pragma mark - 收回搜索栏的第一响应
- (void)resignFirstResponderForSearchBar;
{
    [_seachBar resignFirstResponder];
    _seachBar.text = nil;
}

#pragma mark - 滑动手势的绑定事件
- (void)handlePanGR:(UIPanGestureRecognizer *)sender
{
    
    [self panGRWhenStateNotEnded:sender];
    
    [self panGRWhenStateIsEnded:sender];
    
}

#pragma mark - 正确滑动时执行的内容
- (void)panGRWhenStateNotEnded:(UIPanGestureRecognizer *)sender
{
    
    // 正确滑动时执行的内容
    if (sender.state != UIGestureRecognizerStateEnded && sender.state != UIGestureRecognizerStateFailed) {
        CGPoint point = [sender translationInView:sender.view];
        CGRect rect = sender.view.frame;
        CGFloat newx = sender.view.frame.origin.x + point.x;
        rect.origin = CGPointMake(sender.view.frame.origin.x + point.x, sender.view.frame.origin.y);
        //左滑 和 右滑
        if (_drawerView1.frame.origin.x - newx > 0) {
            //NSLog(@"left");
            if (newx > 70) {
                sender.view.frame = rect;
            }
        }else {
            //NSLog(@"right");
            if (newx < UISCREENWIDTH) {
                sender.view.frame = rect;
            }
        }
        [sender setTranslation:CGPointMake(0, 0) inView:sender.view];
    }

}

#pragma mark - 结束滑动的那一瞬间执行的方法
- (void)panGRWhenStateIsEnded:(UIPanGestureRecognizer *)sender
{
    
    if (sender.state == UIGestureRecognizerStateEnded && sender.state != UIGestureRecognizerStateFailed) {
        if (sender.view.frame.origin.x > 100) {
            [UIView animateWithDuration:0.4 animations:^{
                CGRect rect = sender.view.frame;
                rect.origin.x = UISCREENWIDTH;
                sender.view.frame = rect;
            }];
            [self performSelector:@selector(dismissDrawerView) withObject:nil afterDelay:0.8f];
        } else{
            
            
            [UIView animateWithDuration:0.4 animations:^{
                CGRect rect = sender.view.frame;
                rect.origin.x = 70;
                sender.view.frame = rect;
            }];
            
            
        }
        
    }

}

#pragma mark - 延时执行的方法 tableView收回后，延时收回大透明
- (void)dismissDrawerView
{
    [self resignFirstResponderForSearchBar];
    _drawerView.frame = CGRectMake(UISCREENWIDTH, 0,self.window.frame.size.width, self.window.frame.size.height);
}


#pragma mark - 在scrollView添加7个tableViewController子控制器
- (void)addAllViews
{
    
    self.scrollView = [[[UIScrollView alloc] initWithFrame:CGRectMake(0, 74, UISCREENWIDTH, UISCREENHEIGHT)] autorelease];// 给scrollView设置他改在的长度
    
    [self setAttributeForScrollView:_scrollView];

    [self addTableViewForScrollView:_scrollView];
    
    [self addNavigationForSelfViewAddScrollView:_scrollView];
    
}

#pragma mark - 设置ScrollView的属性
- (void)setAttributeForScrollView:(UIScrollView *)sender
{
    _scrollView.delegate = self;
    _scrollView.contentInset = UIEdgeInsetsMake(-64, 0, 0, 0);// 设置内容视图的距边框的距离
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.bounces = NO;
    _scrollView.pagingEnabled = YES;
    _scrollView.alwaysBounceHorizontal = NO;
    _scrollView.alwaysBounceVertical = NO;
    [self.view addSubview:_scrollView];
    _scrollView.contentSize = CGSizeMake(UISCREENWIDTH * 7, 0);
    
}

#pragma mark - 给ScrollView添加tableView
- (void)addTableViewForScrollView:(UIScrollView *)sender
{
    self.urlArray = [NSMutableArray arrayWithArray:@[kLastNewsURL, kNewsURL, kEvaluatingURL, kShoppingGuideURL, kPriceURL, kRefitURL, kTravelnotesURL]];
   
    for (int i = 0; i < 7; i++) {
        
        InformationTableViewController *informationTVC = [[InformationTableViewController new] autorelease];
        informationTVC.urlIString = _urlArray[i];
        
        informationTVC.tableView.frame = CGRectMake(UISCREENWIDTH * i, 14, UISCREENWIDTH, UISCREENHEIGHT);
        // 如果是行情页面添，则在上方添加view，并在view上添加button
        if (i == 4) {
            
            UIView *provinceView = [[[UIView alloc]initWithFrame:CGRectMake(UISCREENWIDTH * 4, 20, UISCREENWIDTH, 40)]autorelease];
            provinceView.backgroundColor = [UIColor whiteColor];
            [_scrollView addSubview:provinceView];
            
            
            CGRect f = informationTVC.tableView.frame;
            f.origin.y = 60;
            informationTVC.tableView.frame = f;
            UIButton *provinceButton = [UIButton buttonWithType:UIButtonTypeSystem];
            provinceButton.tag = 101;
            provinceButton.frame = CGRectMake(10, 10, 60, 20);
            provinceButton.backgroundColor = [UIColor grayColor];
            [provinceButton setTitle:@"北京" forState:UIControlStateNormal];
            [provinceButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [provinceView addSubview:provinceButton];
            [provinceButton addTarget:self action:@selector(provinceButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        }
        [_scrollView addSubview:informationTVC.tableView];
        [self addChildViewController:informationTVC];// 加上这句话，才会运行informationTVC控制器及其中的方法
    }
}

#pragma mark - 为self.view添加导航栏
- (void)addNavigationForSelfViewAddScrollView:(UIScrollView *)sender
{
    self.allNB = [[[ALLNavigationBar alloc] initWithFrame:CGRectMake(0, 64, UISCREENWIDTH, 30)]autorelease];
    [self.view addSubview:_allNB];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [_allNB.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"indentifier"];
    
    _allNB.scrollView = _scrollView;

    
}

#pragma mark - provinceButton的绑定事件 推出抽屉
- (void)provinceButtonAction:(UIButton *)sender
{
    
    _drawerView1.frame = CGRectMake(70, 0, UISCREENWIDTH - 70, UISCREENHEIGHT);// 因为拖拽时，会改变_drawerView1的frame，但没回推出时，他都要在_drawerView上
    [UIView animateWithDuration:.4 animations:^{
        _drawerView.frame = CGRectMake(0,0,self.window.frame.size.width, self.window.frame.size.height);
    }];
   
    
}

#pragma mark - 滑动scrollView上的视图，让labelItem随之移动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat offset_x = _scrollView.contentOffset.x;
    
    if (offset_x == 4 * UISCREENWIDTH) {
        _allNB.collectionView.contentOffset = CGPointMake(2 * 70, 0);
    }
    
    if (offset_x == 3 * UISCREENWIDTH) {
        _allNB.collectionView.contentOffset = CGPointMake( 0, 0);
    }
    
    CGRect rect = _allNB.labelItem.frame;
    rect.origin.x = 70 * (offset_x / UISCREENWIDTH) + 10;
    [UIView animateWithDuration:0.4 animations:^{
            _allNB.labelItem.frame = rect;
        
    }];
    
}

#pragma mark - 解析文件中的数据
-(void)loadData
{
    self.allProvinceArray = [NSMutableArray array];
    self.allProvinceDict = [NSMutableDictionary dictionary];
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Area" ofType:@"plist"];
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:filePath];
    NSString *str = dict[@"area"];
    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
    for (NSDictionary *di in dic[@"provinces"]) {
        
        Province *p = [[Province new]autorelease];
        [p setValuesForKeysWithDictionary:di];
        [_allProvinceArray addObject:p];
       
    }
    
    for (Province *p in _allProvinceArray) {
        
        NSString *f = p.firstletter;
        if (_allProvinceDict[f] == nil) {
            NSMutableArray *mutableArray = [NSMutableArray arrayWithObjects:p, nil];
            [_allProvinceDict setValue:mutableArray forKey:p.firstletter];
        } else{
            
            [_allProvinceDict[f] addObject:p];
            
        }
        
    }
    
    self.sortFirstletterArray = [NSMutableArray arrayWithArray:_allProvinceDict.allKeys];
    
    [_sortFirstletterArray sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        
        if ([obj1 compare:obj2] == NSOrderedDescending) {
            return YES;
        }
        return NO;
    }];
    
   
}

#pragma mark - 设置有多少分组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if ([tableView isEqual:_drawerProvinceTableView]) {
        return _sortFirstletterArray.count;
    } else {
        return 1;
    }
}

#pragma mark - 设置每组有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([tableView isEqual:_drawerProvinceTableView]) {
        NSString *f = _sortFirstletterArray[section];
        NSArray *a = _allProvinceDict[f];
        return a.count;
    } else {
        return _allCitysArray.count;

    }
    
}

#pragma mark - 设置cell的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if ([tableView isEqual:_drawerProvinceTableView]) {
        
        static NSString *identifier = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        
        if (cell == nil) {
            
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier]autorelease];
        }
        NSString *f = _sortFirstletterArray[indexPath.section];
        NSArray *a = _allProvinceDict[f];
        Province *p = a[indexPath.row];
        
        cell.textLabel.text = p.name;//省名
        return cell;
        
    } else {
        static NSString *identifier2 = @"cell2";//两个tableView不能同用一类cell，和cell上的标示
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier2];
        
        if (cell == nil) {
            
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier2]autorelease];
        }
        City *p = _allCitysArray[indexPath.row];
        cell.textLabel.text = p.name;
        return cell;

    }
  
}

#pragma mark - 设置cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 35;
}

#pragma mark - 设置头标题的内容
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (tableView == _drawerProvinceTableView) {
        
        NSString *f = _sortFirstletterArray[section];
        return f;
    } else{
        return nil;
    }
   
}

#pragma mark - 设置搜索栏
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    if (tableView == _drawerProvinceTableView) {
        
        return _sortFirstletterArray;
    } else{
        return nil;
    }
}

#pragma mark - 设置头标题的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (tableView == _drawerProvinceTableView) {
    
        return 15;
    } else{
        return 0;
    }
}

#pragma mark - 点击cell的触发方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _drawerProvinceTableView) {
        
        //_allCitysArray = nil;//要清空上次数组中保存的数据，不能如上操作，否则会把对象清空的
        NSString *f = _sortFirstletterArray[indexPath.section];
        NSArray *a = _allProvinceDict[f];
        Province *p = a[indexPath.row];
        
        [_allCitysArray removeAllObjects];
        _allCitysArray = [[NSMutableArray alloc] initWithArray:p.citys];
        _drawerCityView.frame = CGRectMake( 0, 0, UISCREENWIDTH - 70, UISCREENHEIGHT);
        [self.drawerCityTableView reloadData];// 要刷新数据
        
    } else{
        City *c = _allCitysArray[indexPath.row];
        [self refreshPageWithCity:c];
        [self swichDrawer];
         [self resignFirstResponderForSearchBar];
        
    }
    
}

#pragma mark - 更新行情页面
-(void)refreshPageWithCity:(City *)city
{
    
    UIButton *button = (UIButton *)[self.scrollView.subviews[4] viewWithTag:101];
    [button setTitle:city.name forState:UIControlStateNormal];
    
    NSString *Id = city.ID;
    NSArray *a = [kPriceURL componentsSeparatedByString:@"-c110100"];
    NSString *strUrl = [a[0] stringByAppendingString:[NSString stringWithFormat:@"-c%@", Id]];
    strUrl = [strUrl stringByAppendingString:a[1]];
    _urlArray[4] = strUrl;
    InformationTableViewController *priceTableViewController = self.childViewControllers[4];
    priceTableViewController.urlIString = strUrl;
    [priceTableViewController loadData];
        
}

@end
